{% extends 'layout/manage.html' %}
{% block css %}
    <style>
        .panel-body {
            padding: 0;
            display: flex;
            flex-direction: row;
            justify-content: left;
            align-items: flex-start;
            flex-wrap: wrap;
        }

        .panel-body > .item {
            border-radius: 6px;
            width: 228px;
            border: 1px solid #dddddd;
            margin: 20px 10px;

        }

        .panel-body > .item:hover {
            border: 1px solid #f0ad4e;
        }

        .panel-body > .item > .title {
            height: 104px;
            color: white;
            display: flex;
            justify-content: center;
            align-items: center;
            border-top-left-radius: 6px;
            border-top-right-radius: 6px;
            font-size: 15px;
            text-decoration: none;
        }

        .panel-body > .item > .info {
            padding: 10px 10px;

            display: flex;
            justify-content: space-between;

            border-bottom-left-radius: 6px;
            border-bottom-right-radius: 6px;
            color: #8c8c8c;

        }

        .panel-body > .item > .info a {
            text-decoration: none;
        }

        .panel-body > .item > .info .fa-star {
            font-size: 18px;
        }

        .color-radio label {
            margin-left: 0;
            padding-left: 0;
        }

        .color-radio input[type="radio"] {
            display: none;
        }

        .color-radio input[type="radio"] + .cycle {
            display: inline-block;
            height: 25px;
            width: 25px;
            border-radius: 50%;
            border: 2px solid #dddddd;
        }

        .color-radio input[type="radio"]:checked + .cycle {
            border: 2px solid #434410;
        }
    </style>
{% endblock %}
{% block content %}
    <div class="container-fluid">
        <div>
            <a class="btn btn-project" id="btnAdd" data-toggle="modal" data-target="#myModal">
                <i class="fa fa-plus" aria-hidden="true" style="margin-right: 5px"></i>新建项目
            </a>
        </div>

        <div class="panel panel-project">
            <div class="panel-heading"><i class="fa fa-star" aria-hidden="true" style="margin-right:5px"></i>星标项目</div>
            <div class="panel-body">
                {% for project in project_list.star %}
                    <div class="item">
                        <a href="{% url 'dashboard' project_id=project.value.id %}" class="title"
                           style="background-color: {{ project.value.get_color_display }}">{{ project.value.name }}</a>
                        <div class="info">
                            <div>
                                <a href="{% url 'project_unstar' project_type=project.type project_id=project.value.id %}">
                                    <i class="fa fa-star" aria-hidden="true" style="margin: 5px;color: #ff8e00"></i>
                                </a>
                                <span>{{ project.value.creator.username }} ({{ project.type }})</span>
                            </div>
                            <div>
                                <i class="fa fa-user-o" aria-hidden="true" style="margin-top: 5px"></i>
                                <span>{{ project.value.join_count }}</span>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>

        <div class="panel panel-project">
            <div class="panel-heading"><i class="fa fa-list" aria-hidden="true" style="margin-right: 5px"></i>我创建的项目
            </div>
            <div class="panel-body">
                {% for project in project_list.my %}
                    <div class="item">
                        <a href="{% url 'dashboard' project_id=project.id %}" class="title"
                           style="background-color: {{ project.get_color_display }}">{{ project.name }}</a>
                        <div class="info">
                            <div>
                                <a href="{% url 'project_star' project_type='my' project_id=project.id %}">
                                    {% if project.star %}
                                        <i class="fa fa-star" aria-hidden="true" style="margin: 5px;color: #ff8e00"></i>
                                    {% else %}
                                        <i class="fa fa-star" aria-hidden="true" style="margin: 5px;color: #8d8d8d"></i>
                                    {% endif %}

                                </a>
                                <span>{{ project.creator.username }}</span>
                            </div>
                            <div>
                                <i class="fa fa-user-o" aria-hidden="true" style="margin-top: 5px"></i>
                                <span>{{ project.join_count }}</span>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>

        <div class="panel panel-project">
            <div class="panel-heading"><i class="fa fa-handshake-o" aria-hidden="true" style="margin-right: 5px"></i>我参与的
            </div>
            <div class="panel-body">
                {% for project in project_list.join %}
                    <div class="item">
                        <a href="{% url 'dashboard' project_id=project.project.id %}" class="title"
                           style="background-color: {{ project.project.get_color_display }}">{{ project.project.name }}</a>
                        <div class="info">
                            <div>
                                <a href="{% url 'project_star' project_type='join' project_id=project.project.id %}">
                                    {% if project.star %}
                                        <i class="fa fa-star" aria-hidden="true" style="margin: 5px;color: #ff8e00"></i>
                                    {% else %}
                                        <i class="fa fa-star" aria-hidden="true" style="margin: 5px;color: #8d8d8d"></i>
                                    {% endif %}
                                </a>
                                <span>{{ project.project.creator.username }}</span>
                            </div>
                            <div>
                                <i class="fa fa-user-o" aria-hidden="true" style="margin-top: 5px"></i>
                                <span>{{ project.project.join_count }}</span>
                            </div>
                        </div>
                    </div>
                {% endfor %}
            </div>
        </div>
    </div>

    <!-- Modal -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog">
        <div class="modal-dialog" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                            aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title">Modal title</h4>
                </div>
                <div class="modal-body">
                    <form id="addform" novalidate>
                        {% csrf_token %}
                        {% for field in form %}
                            <div class="form-group">
                                <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                                {{ field }}
                                <span class="error-msg">{{ form.errors.0 }}</span>
                            </div>

                        {% endfor %}

                    </form>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
                    <button type="button" id='btnSubmit' class="btn btn-project">Save changes</button>
                </div>
            </div><!-- /.modal-content -->
        </div><!-- /.modal-dialog -->
    </div><!-- /.modal -->

{% endblock %}
{% block js %}
    <script>
        $(function () {
            bindBtnSubmit();
            bindBtnAdd();
        })

        function bindBtnSubmit() {
            $('#btnSubmit').click(function () {
                $(".error-msg").empty();
                $.ajax({
                    url: '{% url 'project_list' %}',
                    method: 'post',
                    data: $('#addform').serialize(),
                    contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                    dataType: 'JSON',
                    success: function (res) {
                        console.log(res);
                        if (res.status) {
                            console.log(res);
                            location.href = location.href

                        } else {
                            $.each(res.error, function (key, value) {
                                $("#id_" + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }

        function bindBtnAdd() {
            $('#btnAdd').click(function () {
                // 清除上一次的信息
                $('.error-msg').empty();
                $("#addform")[0].reset();

            })
        }
    </script>
{% endblock %}